package net.alpenviewer.app;
import java.io.File;

import net.alpenviewer.map.SRTM;

/** 
 * VrtGenerate ->  exec batch.1 -> VrtHack -> exec batch.2 -> MergeTiles 
 * */
public class VrtGenerateHelper {

	public static final String VRT_TRANSLATE_EXT = "vrt";	
	public static final String VRT_WARP_EXT      = "vrt-warp";
	public static final String VRT_WARP_TEMP_EXT = VRT_WARP_EXT + ".temp";
	
	private static final File srcSrtmDir = new File("srtm");
	private static final File imageDir   = new File("relief/elevation");

	private static final int maxGmapZoomLevel = 22;

	public static void main(String[] args) throws Exception {
	
		final String mapName = "alpen-viewer-maps";
		
		StringBuffer batch1 = new StringBuffer();
		batch1.append("@echo off").append("\n");
		batch1.append(":: for using gdal6").append("\n");
		batch1.append("\n");
		batch1.append("set MAP_NAME=" + mapName).append("\n");
		batch1.append("\n");		
		
		batch1.append("del *." + VRT_TRANSLATE_EXT).append("\n");
		batch1.append("del *." + VRT_WARP_TEMP_EXT).append("\n");
		batch1.append("del *." + VRT_WARP_EXT).append("\n");
		
		batch1.append("for /D %%d in (*.mercator) do (rd /s /q %%d)").append("\n");
		batch1.append("\n");		
		
		StringBuffer batch2 = new StringBuffer();
		batch2.append("@echo off").append("\n");
		batch2.append(":: for using gdal6").append("\n");
		batch2.append("\n");
		batch2.append("set MAP_NAME=" + mapName).append("\n");		
		
		{
			final File[] files = srcSrtmDir.listFiles();
			for (File file : files) {

				if (file.isFile() && file.getName().toLowerCase().endsWith("." + SRTM.SUPPORTED_FILE_EXT)) {
										
					final SRTM srtm = new SRTM(file);					
					final String elevationFilename = srtm.toString() + ".wgs84.png";
																		
					final File elevationFile = new File(imageDir.getPath() + File.separator + elevationFilename);
					if (!elevationFile.exists()) {
						System.err.println(elevationFile.getAbsolutePath() + " does not exist");
						continue;
					}
					
					System.out.println(srtm.toString());
					
					/* *************** */
					final int size = 1 + 3600 / srtm.arcSecond;
					final String vrtTraslateFilename = srtm.toString() + ".wgs84." + VRT_TRANSLATE_EXT;
					final String vrtWarpFilename     = srtm.toString() + ".mercator." + VRT_WARP_EXT;	
					final String vrtWarpTempFilename = srtm.toString() + ".mercator." + VRT_WARP_TEMP_EXT;
					
					batch1.append("CALL gdal_translate -strict -of VRT -a_srs EPSG:4326 " + 
							"-gcp 0 0 " + srtm.extentNSEW[3] + " " + srtm.extentNSEW[0] + " " +
							"-gcp " + size + " 0 " + srtm.extentNSEW[2] + " " + srtm.extentNSEW[0] + " " +
							"-gcp 0 " + size + " " + srtm.extentNSEW[3] + " " + srtm.extentNSEW[1] + " " + 
							elevationFilename + " " + vrtTraslateFilename).append("\n");
					
					batch1.append("CALL gdalwarp -of VRT -r cubic -s_srs EPSG:4326 -t_srs EPSG:900913 " + vrtTraslateFilename + " " + vrtWarpTempFilename).append("\n");				
					
					batch2.append("CALL gdal2tiles -n -p mercator -z 0-" + maxGmapZoomLevel + " -w google -t %MAP_NAME% -c %MAP_NAME%  " + vrtWarpFilename).append("\n");
					
				}

			}	
		}
		
		batch1.append("PAUSE");
		batch2.append("PAUSE");
		
		final String batchF1 = "maptiles.1.bat";
		final String batchF2 = "maptiles.2.bat";
		
		final File batchFile1 = new File(imageDir.getPath() + File.separator + batchF1);
		StringUtil.writeStringIntoFile(batchFile1, batch1.toString());
		System.out.println(batchF1);
		final File batchFile2 = new File(imageDir.getPath() + File.separator + batchF2);
		StringUtil.writeStringIntoFile(batchFile2, batch2.toString());
		System.out.println(batchF2);
		
	}
	
	

}
